package com.example.cloud.service.business.attendance.impl;

import com.example.cloud.constants.CommonConstants;
import com.example.cloud.dao.business.attendance.MainClassDao;
import com.example.cloud.dao.mongdb.system.MongoUserRepository;
import com.example.cloud.model.personal.MainClass;
import com.example.cloud.model.system.User;
import com.example.cloud.service.business.attendance.MainClassService;
import com.example.cloud.utils.DateUtils;
import com.example.cloud.utils.RequestUtil;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by ygl on 2017/4/9.
 */
@Service
public class MainClassServiceImpl implements MainClassService {

    private static final Logger logger = Logger.getLogger(MainClassServiceImpl.class);

    @Autowired
    private MainClassDao mainClassDao;

    @Autowired
    private MongoUserRepository userRepository;

    @Override
    public MainClass findOne(Long id) {
        return mainClassDao.findOne(id);
    }

    @Override
    public MainClass saveOrUpdate(MainClass record) throws Exception {
        if(record.getUserId() != null){
            User user = userRepository.findOne(Long.valueOf(record.getUserId()));
            if(user != null){
                record.setUserName(user.getUsername());
            }
        }
        record.setAccountId(RequestUtil.getLoginUser().getAccountId());
        return mainClassDao.save(record);
    }

    @Override
    public List<List<MainClass>> findAllByCurrentDate(Date date, User user) {
        String today = DateUtils.DATE_FORMAT.format(new Date());
        List<List<MainClass>> resultList = new ArrayList<List<MainClass>>();
        List<MainClass> list = new ArrayList<MainClass>();
        try {
            Date firstDay = DateUtils.DATE_FORMAT.parse(DateUtils.getFirstDay(date));
            int week = DateUtils.getWeek(firstDay);
            if(week == 0){
                for (int i = 0; i < 6; i++) {
                    MainClass mainClass = new MainClass();
                    mainClass.setDay(-1);
                    list.add(mainClass);
                }
            }else if(week == 2){
                for (int i = 0; i < 1; i++) {
                    MainClass mainClass = new MainClass();
                    mainClass.setDay(-1);
                    list.add(mainClass);
                }
            }else if(week == 3){
                for (int i = 0; i < 2; i++) {
                    MainClass mainClass = new MainClass();
                    mainClass.setDay(-1);
                    list.add(mainClass);
                }
            }else if(week == 4){
                for (int i = 0; i < 3; i++) {
                    MainClass mainClass = new MainClass();
                    mainClass.setDay(-1);
                    list.add(mainClass);
                }
            }else if(week == 5){
                for (int i = 0; i < 4; i++) {
                    MainClass mainClass = new MainClass();
                    mainClass.setDay(-1);
                    list.add(mainClass);
                }
            }else if(week == 6){
                for (int i = 0; i < 5; i++) {
                    MainClass mainClass = new MainClass();
                    mainClass.setDay(-1);
                    list.add(mainClass);
                }
            }
            String strStartTime = DateUtils.getFirstDay(date);
            strStartTime += " 00:00:00";
            String strEndTime = DateUtils.getLastDay(date);
            strEndTime += " 23:59:59";
            Date startTime = DateUtils.DATETIME_FORMAT.parse(strStartTime);
            Date endTime = DateUtils.DATETIME_FORMAT.parse(strEndTime);
            Map<String, MainClass> resultMap = findAllByMap(user.getAccountId(), startTime, endTime);
            int endDayNumber = DateUtils.getLastDayToNumber(date);
            for (int i = 0; i < endDayNumber; i++) {
                Date monthDay = DateUtils.addDays(firstDay, i);
                MainClass mainClass = new MainClass();
                mainClass.setUserName("");
                mainClass.setDutyDate(monthDay);
                String strMonthDay = DateUtils.DATE_FORMAT.format(monthDay);
                if(resultMap.containsKey(strMonthDay)){
                    mainClass = resultMap.get(strMonthDay);
                }
                mainClass.setDay(DateUtils.getDay(monthDay));
                if(strMonthDay.equals(today)){
                    mainClass.setIsDay(1);// 表示为当天
                }
                list.add(mainClass);
                if(list.size() == 7){
                    resultList.add(list);
                    list = new ArrayList<MainClass>();
                }
            }
            if(list.size() > 0){
                if(list.size() == 1){
                    for (int i = 0; i < 6; i++) {
                        MainClass mainClass = new MainClass();
                        mainClass.setDay(-1);
                        list.add(mainClass);
                    }
                }else if(list.size() == 2){
                    for (int i = 0; i < 5; i++) {
                        MainClass mainClass = new MainClass();
                        mainClass.setDay(-1);
                        list.add(mainClass);
                    }
                }else if(list.size() == 3){
                    for (int i = 0; i < 4; i++) {
                        MainClass mainClass = new MainClass();
                        mainClass.setDay(-1);
                        list.add(mainClass);
                    }
                }else if(list.size() == 4){
                    for (int i = 0; i < 3; i++) {
                        MainClass mainClass = new MainClass();
                        mainClass.setDay(-1);
                        list.add(mainClass);
                    }
                }else if(list.size() == 5){
                    for (int i = 0; i < 2; i++) {
                        MainClass mainClass = new MainClass();
                        mainClass.setDay(-1);
                        list.add(mainClass);
                    }
                }else if(list.size() == 6){
                    for (int i = 0; i < 1; i++) {
                        MainClass mainClass = new MainClass();
                        mainClass.setDay(-1);
                        list.add(mainClass);
                    }
                }
                resultList.add(list);
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return resultList;
    }

    public Map<String, MainClass> findAllByMap(Long accountId, Date startTime, Date endTime) {
        Map<String, MainClass> resultMap = new HashMap<String, MainClass>();
        List<MainClass> list = mainClassDao.findAllByAccountIdAndDutyDateBetween(accountId, startTime,endTime);
        for (MainClass mainClass : list) {
            String currentDate = DateUtils.DATE_FORMAT.format(mainClass.getDutyDate());
            resultMap.put(currentDate, mainClass);
        }
        return resultMap;
    }
}
